package com.walixiwa.snackdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.snackdialog.R;

public class SnackDialog extends Dialog {
    private TextView title;
    private TextView subTitle;
    private ProgressBar progressBar;

    public SnackDialog(Context context) {
        super(context, R.style.SnackDialogStyle);
        View content = View.inflate(context, R.layout.snack_dialog, null);
        this.title = content.findViewById(R.id.title);
        this.progressBar = content.findViewById(R.id.progress);
        this.subTitle = content.findViewById(R.id.subTitle);
        this.setContentView(content);
    }

    public SnackDialog setTitle(String title) {
        this.title.setText(title);
        return this;
    }

    public SnackDialog setSubTitle(String subTitle) {
        this.subTitle.setText(subTitle);
        return this;
    }

    public SnackDialog setIndeterminate(boolean b) {
        this.progressBar.setIndeterminate(b);
        return this;
    }

    public SnackDialog setProgress(int progress) {
        this.progressBar.setProgress(progress);
        return this;
    }

    public SnackDialog setAutoCancelable(boolean flag) {
        this.setCancelable(flag);
        return this;
    }

    @Override
    public void show() {
        super.show();
        //设置弹窗在底部
        Window window = this.getWindow();
        if (window != null) {
            Display display = window.getWindowManager().getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) (point.x * 1.0);
            this.getWindow().setGravity(Gravity.BOTTOM);
        }
    }
}

